﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Xml;

namespace autobrowser
{
    public partial class frm_modify : Form
    {
        public frm_modify()
        {
            InitializeComponent();
        }

        private void urlmodify_Load(object sender, EventArgs e)
        {
            if (!System.IO.File.Exists("urls.xml")) createxml();
            ds.ReadXml("urls.xml");
            dataGridView1.DataSource = ds.Tables[0];
            dataGridView1.Columns[0].HeaderText = "组名";
            dataGridView1.Columns[0].Width = 100;
            dataGridView1.Columns[1].HeaderText = "网站名";
            dataGridView1.Columns[1].Width = 100;
            dataGridView1.Columns[2].HeaderText = "网站地址";
            dataGridView1.Columns[2].Width = 300;
        }

        private void urlmodify_FormClosing(object sender, FormClosingEventArgs e)
        {
            ds.WriteXml("urls.xml");
        }

        private void toolStripButton1_Click(object sender, EventArgs e)  //menu clear button
        {
            ds.Clear();
            createxml();
        }

        private void createxml()
        {
            XmlDocument doc = new XmlDocument();

            XmlDeclaration xmlDec = doc.CreateXmlDeclaration("1.0", "gb2312", null);

            XmlElement urls = doc.CreateElement("URLS");
            doc.AppendChild(urls);


            XmlElement url = doc.CreateElement("URL");

            XmlElement ug = doc.CreateElement("UG");
            ug.InnerText = " ";
            url.AppendChild(ug);

            XmlElement un = doc.CreateElement("UN");
            un.InnerText = " ";
            url.AppendChild(un);

            XmlElement ua = doc.CreateElement("UA");
            ua.InnerText = " ";
            url.AppendChild(ua);

            XmlElement root = doc.DocumentElement;
            root.AppendChild(url);
            doc.InsertBefore(xmlDec, root);
            doc.Save("urls.xml");
            ds.ReadXml("urls.xml");
        }
    }
}
